// packageProduct/pages/detail/nv/nv.js
import {getWeekNv} from "../../../../service/Product";
import {accLess, numberToFixed} from "../../../../utils/Util";
Component({
    /*
    * 接收外部样式
    * */
    options: {
        addGlobalClass: true
    },

    /**
     * 组件的属性列表
     */
    properties: {
        halfYear: {
            type: String,
            value: ""
        },
        year: {
            type: String,
            value: ""
        },
        all: {
            type: String,
            value: ""
        },
        productId: {
            type: String,
            value: ""
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        _tabs: [{
            value: 1,
            name: "近半年",
        }, {
            value: 2,
            name: "近一年",
        }, {
            value: 0,
            name: "成立以来",
        }],
        _yearValue: "", //收益率
        _current: 0,
        _list: [],
        _page: 1,
        _pageSize: 5,
        _showLoading: true,
        _showMore: false
    },

    /*
    * 数据监听
    * */
    observers: {
        "productId": function (productId) {
            if (productId) {
                this.getNvList(productId)
            }
        },
        "halfYear": function (value) {
            if (value) {
                this.setData({_yearValue: "近半年收益率" + value})
            }
        }
    },

    /**
     * 组件的方法列表
     */
    methods: {
        /*
        * tabs 切换
        * */
        onTabsChange: function (e) {
            const that = this;
            let halfYear = this.data.halfYear;
            let year = this.data.year;
            let all = this.data.all;
            clearTimeout(that._onTabsChange);
            that._onTabsChange = setTimeout(function () {
                let {index} = e.detail;
                let current = that.data._current;
                if (index !== current) {
                    that.data._current = index;
                    that.setData({
                        _page: 1,
                        _list: [],
                        _showLoading: true,
                        _showMore: false,
                        _yearValue: (function () {
                            if (index === 0) {
                                return "近半年收益率" + halfYear
                            } else if (index === 1) {
                                return "近一年收益率" + year
                            } else if (index === 2) {
                                if (!all) return "";
                                let _val = accLess(all, 1, true);
                                return "成立以来收益率" + numberToFixed(_val) + "%"
                            }
                        })()
                    })
                    that.getNvList();
                }
            }, 300)
        },
        /*
        * 获取净值信息
        * */
        getNvList: async function () {
            try {
                let id = this.data.productId;
                let tabs = this.data._tabs;
                let current = this.data._current;
                let pageSize = this.data._pageSize;
                let res = await getWeekNv({
                    c_fund_id: id,
                    type: tabs[current].value
                })
                this._nvList = res;
                this.setData({
                    _list: res.slice(0, pageSize),
                    _showMore: res.length > pageSize
                })
            } catch (e) {
                console.error(e)
            } finally {
                this.setData({_showLoading: false})
            }
        },

        /*
        * 展示更多
        * */
        bindShowMore: function () {
            let allList = this._nvList;
            let list = this.data._list;
            let page = this.data._page;
            let pageSize = this.data._pageSize;
            list = list.concat(allList.slice(page * pageSize, (page + 1) * pageSize));
            this.data._page += 1;
            this.setData({
                _list: list,
                _showMore: allList.length !== list.length
            });
        }
    }
})
